import x2js from 'x2js'
import axios from 'axios'
var ObjTree = require('objtree/index')
/**
 * 正则表达式数据清洗1
 */
function transLocationData(str) {
  const reg = /\((.+?)\)/;
  const rawstr = str.match(reg)[1];
  const xy = rawstr.split(',');
  const locationslist = xy.map((item) => {
    return item.split(' ');
  });
  return JSON.stringify(locationslist);
}
/**
 * 正则表达式数据清洗2
 */
function transLocationData2(str) {
  const reg = /\((.+?)\)/;
  const rawstr = str.match(reg)[1];
  const xy = rawstr.split(' ');
  return JSON.stringify(xy);
}


/**
 * 解析 ACLINE
 */
function parseACLINE(raw) {
  //task 1-找出 isCurrentCircuit 属性为 true 的 Naming.name
  let circuit_name;
  if (!raw["rdf:RDF"]['cim:Circuit'].length) {
    circuit_name = raw["rdf:RDF"]['cim:Circuit']['cim:Naming.name'];
  } else {
    for (var x = 0; x < raw["rdf:RDF"]['cim:Circuit'].length; x++) {
      if (raw["rdf:RDF"]['cim:Circuit'][x]['cim:Circuit.isCurrentCircuit'] == 'true') {
        circuit_name = raw["rdf:RDF"]['cim:Circuit'][x]['cim:Naming.name'];
        break;
      }
    }
  }
  //task 2-提取 cim:ACLineSegment的属性
  let params = raw["rdf:RDF"]["cim:ACLineSegment"].map((item) => {
    return {
      rdfID: item['-rdf:ID'],
      CircuitSectionName: circuit_name,
      ACLineSegment_MemberOf_SegmentGroup: item['cim:ACLineSegment.MemberOf_SegmentGroup'],
      Conductor_crossSectionArea: item['cim:Conductor.crossSectionArea'],
      Conductor_length: item['cim:Conductor.length'],
      Equipment_MemberOf_CircuitSection: JSON.stringify(item['cim:Equipment.MemberOf_CircuitSection']),
      Equipment_MemberOf_EquipmentContainer: JSON.stringify(item['cim:Equipment.MemberOf_EquipmentContainer']),
      Equipment_model: item['cim:Equipment.model'],
      Naming_mRID: item['cim:Naming.mRID'],
      Naming_name: item['cim:Naming.name'],
      PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
      PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
      PowerSystemResource_location: transLocationData(item['cim:PowerSystemResource.location']),
      PowerSystemResource_status: item['cim:PowerSystemResource.status']
    }
  });
 return params;
}


/**
 * 解析 SUBSTATION
 */
function parseSubstation(raw) {
  let circuit_name;
  if (!raw["rdf:RDF"]['cim:Circuit'].length) {
    circuit_name = raw["rdf:RDF"]['cim:Circuit']['cim:Naming.name'];
  } else {
    for (var x = 0; x < raw["rdf:RDF"]['cim:Circuit'].length; x++) {
      if (raw["rdf:RDF"]['cim:Circuit'][x]['cim:Circuit.isCurrentCircuit'] === 'true') {
        circuit_name = raw["rdf:RDF"]['cim:Circuit'][x]['cim:Naming.name'];
        break;
      }
    }
  }
  //task 3-获取Substation下的属性
  let params = raw["rdf:RDF"]["cim:Substation"].map((item) => {
    return {
      rdfID: item['-rdf:ID'],
      CircuitSectionName: circuit_name,
      Naming_mRID: item['cim:Naming.mRID'],
      Naming_name: item['cim:Naming.name'],
      PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
      PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
      PowerSystemResource_location: item['cim:PowerSystemResource.location'] != null ? transLocationData2(item['cim:PowerSystemResource.location']) : '',
      PowerSystemResource_status: item['cim:PowerSystemResource.status'],
      PowerSystemResource_subType: item['cim:PowerSystemResource_subType'],
      PowerSystemResource_symbolRotation: item['cim:PowerSystemResource_symbolRotation'],
      Substation_MemberOf_Circuit: JSON.stringify(item['cim:Substation_MemberOf_Circuit']),
      Substation_MemberOf_CircuitSection: JSON.stringify(item['cim:Substation_MemberOf_CircuitSection'])
    }
  });
  return params;
}

/**
 * 上传circuit
 */
function parseCircuit(raw) {
  let params;
  //task 4-解析出 Circuit 数据
  if (raw["rdf:RDF"]['cim:Circuit'].length) {
    params = raw["rdf:RDF"]['cim:Circuit'].map((item) => {
      return {
        rdfID: item['-rdf:ID'],
        Circuit_SourceBreaker: JSON.stringify(item['cim:Circuit.SourceBreaker']),
        Circuit_SourceSubst: JSON.stringify(item['cim:Circuit.SourceSubst']),
        Circuit_isCurrentCircuit: item['cim:Circuit.isCurrentCircuit'],
        Naming_mRID: item['cim:Naming.mRID'],
        Naming_name: item['cim:Naming.name'],
        PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
        PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
        PowerSystemResource_SubControlArea: JSON.stringify(item['cim:PowerSystemResource.SubControlArea']),
        PowerSystemResource_areaFeature: item['cim:PowerSystemResource.areaFeature'],
        PowerSystemResource_status: item['cim:PowerSystemResource.status'],
        SvgFileName: raw['filename']
      }
    });
  } else {
    let item = raw["rdf:RDF"]['cim:Circuit'];
    params = [{
      rdfID: item['-rdf:ID'],
      Circuit_SourceBreaker: JSON.stringify(item['cim:Circuit.SourceBreaker']),
      Circuit_SourceSubst: JSON.stringify(item['cim:Circuit.SourceSubst']),
      Circuit_isCurrentCircuit: item['cim:Circuit.isCurrentCircuit'],
      Naming_mRID: item['cim:Naming.mRID'],
      Naming_name: item['cim:Naming.name'],
      PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
      PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
      PowerSystemResource_SubControlArea: JSON.stringify(item['cim:PowerSystemResource.SubControlArea']),
      PowerSystemResource_areaFeature: item['cim:PowerSystemResource.areaFeature'],
      PowerSystemResource_status: item['cim:PowerSystemResource.status'],
      SvgFileName: raw['filename']
    }]
  }
 return params;
}

/**
 * 解析 PowerTransformer
 */
function parsePowerTransformer(raw) {
  let circuit_name;
  if (!raw["rdf:RDF"]['cim:Circuit'].length) {
    circuit_name = raw["rdf:RDF"]['cim:Circuit']['cim:Naming.name'];
  } else {
    for (var x = 0; x < raw["rdf:RDF"]['cim:Circuit'].length; x++) {
      if (raw["rdf:RDF"]['cim:Circuit'][x]['cim:Circuit.isCurrentCircuit'] == 'true') {
        circuit_name = raw["rdf:RDF"]['cim:Circuit'][x]['cim:Naming.name'];
        break;
      }
    }
  }
  //task 5-解析出变压器数据
  let params = raw["rdf:RDF"]["cim:PowerTransformer"].map((item) => {
    return {
      rdfID: item['-rdf:ID'],
      CircuitSectionName: circuit_name,
      Naming_mRID: item['cim:Naming.mRID'],
      Naming_name: item['cim:Naming.name'],
      Equipment_MemberOf_EquipmentContainer: JSON.stringify(item['cim:Equipment.MemberOf_EquipmentContainer']),
      Equipment_model: item['cim:Equipment.model'],
      PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
      PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
      PowerSystemResource_SupplyCircuit: item['cim:PowerSystemResource.SupplyCircuit'],
      PowerSystemResource_dispatchNumber: item['cim:PowerSystemResource.dispatchNumber'],
      PowerSystemResource_location: item['cim:PowerSystemResource.location'] != null ? transLocationData2(item['cim:PowerSystemResource.location']) : '',
      PowerSystemResource_status: item['cim:PowerSystemResource.status'],
      PowerSystemResource_subType: item['cim:PowerSystemResource.subType'],
      PowerSystemResource_symbolRotation: item['cim:PowerSystemResource.symbolRotation'],
      PowerTransformer_MainSupplyCircuit: JSON.stringify(item['cim:PowerTransformer.MainSupplyCircuit']),
      PowerTransformer_mK_ID: item['cim:PowerTransformer.mK_ID'],
      PowerTransformer_ratedCapacity: item['cim:PowerTransformer.ratedCapacity'],
      PowerTransformer_transformer_TYPE: item['cim:PowerTransformer.transformer_TYPE'],
      PowerTransformer_userCode: item['cim:PowerTransformer.userCode']
    }
  });
  return params;
}

/**
 * 解析 switch
 */
function parseSwitch(raw) {
  let circuit_name;
  if (!raw["rdf:RDF"]['cim:Circuit'].length) {
    circuit_name = raw["rdf:RDF"]['cim:Circuit']['cim:Naming.name'];
  } else {
    for (var x = 0; x < raw["rdf:RDF"]['cim:Circuit'].length; x++) {
      if (raw["rdf:RDF"]['cim:Circuit'][x]['cim:Circuit.isCurrentCircuit'] == 'true') {
        circuit_name = raw["rdf:RDF"]['cim:Circuit'][x]['cim:Naming.name'];
        break;
      }
    }
  }
  let params;
  //task 6-解析出开关数据
  if (raw["rdf:RDF"]["cim:LoadBreakSwitch"].length) {
    params = raw["rdf:RDF"]["cim:LoadBreakSwitch"].map((item) => {
      return {
        rdfID: item['-rdf:ID'],
        CircuitSectionName: circuit_name,
        Naming_mRID: item['cim:Naming.mRID'],
        Naming_name: item['cim:Naming.name'],
        Equipment_MemberOf_EquipmentContainer: JSON.stringify(item['cim:Equipment.MemberOf_EquipmentContainer']),
        PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
        PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
        PowerSystemResource_SupplyCircuit: item['cim:PowerSystemResource.SupplyCircuit'],
        PowerSystemResource_dispatchNumber: item['cim:PowerSystemResource.dispatchNumber'],
        PowerSystemResource_location: item['cim:PowerSystemResource.location'] != null ? transLocationData2(item['cim:PowerSystemResource.location']) : '',
        PowerSystemResource_status: item['cim:PowerSystemResource.status'],
        PowerSystemResource_subType: item['cim:PowerSystemResource.subType'],
        PowerSystemResource_symbolRotation: item['cim:PowerSystemResource.symbolRotation'],
        Switch_cabinetNumber: item['cim:Switch.cabinetNumber'],
        Switch_isRing: item['cim:Switch.isRing'],
        Switch_normalOpen: item['cim:Switch.normalOpen'],
        Switch_normalPosition: item['cim:Switch.normalPosition']
      }
    });
  } else {
    let item = raw["rdf:RDF"]["cim:LoadBreakSwitch"];
    params = [{
      rdfID: item['-rdf:ID'],
      CircuitSectionName: circuit_name,
      Naming_mRID: item['cim:Naming.mRID'],
      Naming_name: item['cim:Naming.name'],
      Equipment_MemberOf_EquipmentContainer: JSON.stringify(item['cim:Equipment.MemberOf_EquipmentContainer']),
      PowerSystemResource_BaseVoltage: JSON.stringify(item['cim:PowerSystemResource.BaseVoltage']),
      PowerSystemResource_PSRType: JSON.stringify(item['cim:PowerSystemResource.PSRType']),
      PowerSystemResource_SupplyCircuit: item['cim:PowerSystemResource.SupplyCircuit'],
      PowerSystemResource_dispatchNumber: item['cim:PowerSystemResource.dispatchNumber'],
      PowerSystemResource_location: item['cim:PowerSystemResource.location'] != null ? transLocationData2(item['cim:PowerSystemResource.location']) : '',
      PowerSystemResource_status: item['cim:PowerSystemResource.status'],
      PowerSystemResource_subType: item['cim:PowerSystemResource.subType'],
      PowerSystemResource_symbolRotation: item['cim:PowerSystemResource.symbolRotation'],
      Switch_cabinetNumber: item['cim:Switch.cabinetNumber'],
      Switch_isRing: item['cim:Switch.isRing'],
      Switch_normalOpen: item['cim:Switch.normalOpen'],
      Switch_normalPosition: item['cim:Switch.normalPosition']
    }]
  }
 return params;
}


/**
 * xml 文件转换为 json
 */

async function xml2Json(){
  const xotree = new ObjTree()
  const ret = await axios.get("/data.xml")
  return xotree.parseXML(ret.data);
  // return new x2js().xml2js(ret.data)
}

export{
  xml2Json,
  parseSwitch,
  parseCircuit,
  parseSubstation,
  parseACLINE,
  parsePowerTransformer
}
